<?php

namespace app\controller;


use app\service\WxShopService;
use app\service\YouYinService;
use support\Http;
use support\Redis;
use support\Request;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class IndexController
{
    protected array $noNeedLogin = ['index', 'test', 'notify'];

    public function index(Request $request)
    {


        return view('index/view', ['name' => 'webman']);
    }

    public function notify(Request $request)
    {
        $token = 'qq19951202'; // 在微信后台填写的Token值
        var_dump($request->all());
        $signature = $request->get('signature');
        $timestamp = $request->get('timestamp');
        $nonce = $request->get('nonce');
        $echostr = $request->get('echostr');

        $tmpArr = [$token, $timestamp, $nonce];
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr === $signature) {
            return response($echostr); // 验证通过，返回 echostr
        } else {
            return response('verify fail'); // 验证失败
        }


    }


    //查询当前可用分机信息
    public function test(Request $request)
    {
        \app\service\DnService::cleanupExpired();

        // 获取可用分机号数量
        $availableCount = Redis::sCard('dn_pool_set');

        // 获取已绑定分机号数量
        $bindingCount = Redis::zCard('dn_bind_zset');

        // 可用分机号示例（随机取10个）
        $examples = [];
        if ($availableCount > 0) {
            // Redis Set没有直接的限制返回数量命令，只能用sRandMember获取若干随机元素
            $examples = Redis::sRandMember('dn_pool_set', 10);
        }

        var_dump("当前可用分机号数量：$availableCount");
        var_dump("当前已绑定分机号数量：$bindingCount");


        return json([]);


        /*$res =    (new WxShopService())->wxShopOrderUpdate('channels_ec_order_pay', '3729872309645358848');

        return json($res);*/

    }

    //使用教程
    public function tutorial(Request $request)
    {
        $list = [
            [
                'desc' => '1.微信扫码绑定信息',
                'imgUrl' => 'https://hennan-sanjiaomao.oss-cn-beijing.aliyuncs.com/uploads/jc/1.jpg',
            ],
            [
                'desc' => '2.揭开表层保护膜',
                'imgUrl' => 'https://hennan-sanjiaomao.oss-cn-beijing.aliyuncs.com/uploads/jc/2.jpg',
            ],
            [
                'desc' => '3.从车内贴在前挡风上',
                'imgUrl' => 'https://hennan-sanjiaomao.oss-cn-beijing.aliyuncs.com/uploads/jc/3.jpg',
            ],
            [
                'desc' => '4.启用成功',
                'imgUrl' => 'https://hennan-sanjiaomao.oss-cn-beijing.aliyuncs.com/uploads/jc/4.jpg',
            ]
        ];


        $res = [
            'is_use' => false,
            'list' => $list
        ];


        return json(apiSuccess($res));

    }


    public function json(Request $request)
    {
        $res = (new Http())->get('https://www.baidu.com', [
            'wd' => 'aaa',
        ]);
        return json(apiSuccess([$res]));
    }
}
